home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / serial / callback.001 / callback~ / callback / lib / build next >
Text File  |  1996-07-23  |  11KB  |  458 lines

  1. #!/usr/local/bin/icmake -qt /tmp/lib
  2.  
  3. // CLASSES: the list of directories containing the sources of classes. E.g.,
  4. //      #define CLASSES "class1 class2"
  5.  
  6. string
  7.     CLASSES;
  8.  
  9. void setclasses()
  10. {
  11.     CLASSES =   "error mem log config dbase parser startup ttyline "
  12.                 "dgroup dest dgroup user dname process "
  13.             ;
  14. }
  15.  
  16.  
  17. /*                              build
  18.  
  19.     This icmake script will create the librss.a library, containing
  20.     rss-functions for the callback programs
  21.  
  22. Configurable defines for the build script:
  23.  
  24.     BIFLEX:         only to be defined if the biflex-script will be used.
  25.     CLASSES:        string of directory-names under which sources of classes
  26.                     are found
  27.     COMPILER:       "g++" for C++ sources. Do not change this, unless you're
  28.                     sure you want something else.
  29.     COPT:           C-options used by COMPILER
  30.     ECHO_REQUEST:   ON (default) if command echoing is wanted, otherwise: set 
  31.                     to OFF
  32.     GDB:            define if gdb-symbolic debug information is wanted
  33.                     (not defined by default)
  34.     LIBS:           Extra libraries used for linking
  35.     LIBPATH:        Extra library-searchpaths used for linking
  36.     INTERACTIVE:    define only if, in the context of BIFLEX, an interactive
  37.                     scanner is wanted
  38.     NO_LIBRARY:     define this if you don't want a library for the object
  39.                     modules, but want to keep them in separate ./o directories
  40.             by default: not defined, so a library is used.
  41.     PROGRAM:        define if a program is to be built. If not defined,
  42.                     library maintenance is assumed.
  43.                     (default: defined)
  44.                     
  45.     Current values:
  46. */
  47.  
  48. #define BIFLEX
  49.  
  50. //Do not change the next define unless you know what you're doing:
  51. #define COMPILER "gcc"
  52.  
  53. // COPT: the set of C-options
  54. #define COPT "-m486 -Wall -funsigned-char"
  55.  
  56. #define ECHO_REQUEST ON
  57.  
  58. // #define GDB
  59. // #define INTERACTIVE
  60.  
  61. // Extra libraries required. Remove lib and .a from the library names.
  62. // E.g., #define LIBS "m Xt" to link libm.a and libXt.a explicitly
  63. // Specify libs from the most specific to the most general one.
  64. #define LIBS ""
  65.  
  66. // Extra library-paths required. 
  67. // E.g., #define LIBPATH "/some/path /some/other/path" to search these paths
  68. // apart from the default paths
  69. #define LIBPATH ""
  70.  
  71. // #define NO_LIBRARY
  72. // #define PROGRAM
  73.  
  74.  
  75. //      DO NOT ALTER THINGS BELOW THIS LINE
  76. string                                      // contain options for
  77.     libs,                                // extra libs, e.g., "-lrss -licce"
  78.     libpath,                       // extra lib-paths, eg, "-L../rss"
  79.     copt,
  80.     lopt,
  81.     libxxxa;                    // expanded lib-name
  82. int
  83.     relink;                                 // internally used: != 0 to relink
  84.  
  85. string
  86.     ofiles,                             // wildcards for o-files
  87.     sources,                // sources to be used
  88.     wild,                               // wildcard of extension
  89.     current;                            // contains name of current dir.
  90. /*
  91.                                 O B J F I L E S . I M
  92. */
  93.  
  94. list objfiles(list files)
  95. {
  96.     string
  97.         file,
  98.         objfile;
  99.     int
  100.         i;
  101.  
  102.     for (i = 0; i < sizeof(files); i++)
  103.     {
  104.         file = element(i, files);           // determine element of the list
  105. #ifdef NO_LIBRARY
  106.         objfile = "./o/" + change_ext(file, "o");    // make obj-filename
  107. #else
  108.         objfile = change_ext(file, "o");    // make obj-filename
  109. #endif
  110.         if (objfile younger file)           // objfile is younger
  111.         {
  112.             files -= (list)file;            // remove the file from the list
  113.             i--;                            // reduce i to test the next
  114.         }
  115.     }
  116.     return (files);
  117. }
  118. /*
  119.                                  biflex.im
  120. */             
  121.  
  122. void biflex()
  123. {
  124.     chdir("biflex");
  125.     
  126.     if ("parser" younger "parser.tab.c")        // new parser needed
  127.     {
  128.         exec("bison", "-d", "parser");
  129.         printf("Note: the compilation of parser.c may produce "
  130.            "several compiler warnings.\n");
  131.     }
  132.         
  133.     if 
  134.     (                                           // new lexer needed
  135.         "lexer" younger "lex.yy.c"
  136.         ||
  137.         "parser.tab.h" younger "lex.yy.c" 
  138.     )
  139.     {
  140.         exec("flex",
  141. #ifdef INTERACTIVE
  142.                     "-I", 
  143. #endif
  144.                     "lexer");
  145.                     
  146.         printf("Note: the compilation of lexer.c may produce "
  147.            "several compiler warnings.\n");
  148.     }
  149.                     
  150.     chdir("..");
  151. }
  152.  
  153. /*
  154.                                 A L T E R E D . I M
  155. */
  156.  
  157. list altered(list files, string target)
  158. {
  159.     int
  160.         i;
  161.     string
  162.         file;
  163.  
  164.     for (i = 0; i < sizeof(files); i++)     // try all elements of the list
  165.     {
  166.         file = element(i, files);           // use element i of the list
  167.             
  168.         if (file older target)              // a file is older than the target
  169.         {
  170.             files -= (list)file;            // remove the file from the list
  171.             i--;                            // reduce i to inspect the next
  172.         }                                   // file of the list
  173.     }
  174.     return (files);                         // return the new list
  175. }
  176. /*
  177.                             F I L E L I S T . I M
  178. */
  179.  
  180. list file_list(string type, string library)
  181. {
  182.     list
  183.         files;
  184.  
  185.     files = makelist(type);                 // make all files of certain type
  186. #ifndef NO_LIBRARY
  187.     files = altered(files, library);        // keep all files newer than lib.
  188. #endif
  189.     files = objfiles(files);                // remove if younger .obj exist
  190.  
  191.     return (files);
  192. }
  193. /*
  194.                         L I N K . I M
  195. */
  196.  
  197. void link(string library, string exe)
  198. {
  199.     if
  200.     (
  201.         relink                           // new library, new main.obj
  202.         ||
  203.         !exists(exe)                     // final program doesn't exist
  204.     )
  205.     {
  206.         printf("\n");
  207.         exec(COMPILER, "-o", exe, 
  208. #ifdef NO_LIBRARY
  209.             ofiles,
  210. #else
  211.         "-l" + library, 
  212. #endif
  213.         libs, "-L.", libpath, lopt);
  214. #ifndef GDB
  215.         exec("strip", exe);
  216. #endif
  217.         printf("ok: ", exe, "\n");
  218.     }
  219. }
  220. /*
  221.                             C C O M P I L E . I M
  222. */
  223.  
  224. void c_compile(list cfiles)
  225. {
  226.     string
  227.         nextfile;
  228.     int
  229.         i;
  230.                 
  231. #ifdef NO_LIBRARY
  232.     if (!exists("o"))
  233.         system("mkdir o");
  234.     
  235.     chdir ("o");    
  236. #endif
  237.                                                       
  238.     if (sizeof(cfiles))            // files to compile ?
  239.     {
  240.         printf("\ncompiling: ", current, "\n\n");
  241.                     // compile all files separately
  242.     for (i = 0; nextfile = element(i, cfiles); i++)
  243.         exec(COMPILER,
  244.         "-c "
  245.         COPT + " " +
  246. #ifdef NO_LIBRARY
  247.         copt + " ../" + nextfile);
  248. #else
  249.         copt + " " + nextfile);
  250. #endif
  251.  
  252.            relink = 1;
  253.     printf("\n");
  254.     }
  255.  
  256. #ifdef NO_LIBRARY
  257.     chdir("..");
  258. #endif
  259.                                                       
  260.     printf("ok: ", current, "\n");
  261. }
  262. /*
  263.                             U P D A T E L I . I M
  264. */
  265.  
  266. void updatelib(string library)
  267. {
  268.     list
  269.     arlist,
  270.         objlist;
  271.     string
  272.         to,
  273.         from;
  274.  
  275.     objlist = makelist("*.o");
  276.  
  277.     if (!sizeof(objlist))
  278.         return;
  279.  
  280.     printf("\n");
  281.     relink = 1;
  282.  
  283.     exec("ar", "rvs", library, "*.o");
  284.     exec("rm", "*.o");
  285.  
  286.     printf("\n");
  287. }
  288.  
  289. void prefix_class(string class_id)
  290. {
  291.     list
  292.     o_files;
  293.     string
  294.     o_file;
  295.     int
  296.     i;
  297.  
  298.     o_files = makelist("*.o");
  299.  
  300.     for (i = 0; o_file = element(i, o_files); i++)
  301.     exec("mv", o_file, class_id + o_file);
  302. }
  303. /*
  304.                                 S T D C P P . I M
  305. */
  306.  
  307. void std_cpp(string library)
  308. {
  309.     list
  310.         cfiles;
  311.  
  312. #ifdef BIFLEX
  313.     if (exists("biflex"))                  // subdir biflex exists
  314.         biflex();                          // make recent files
  315. #endif                       
  316.  
  317.     cfiles = file_list(wild, library);      // make list of all cpp-files
  318.  
  319.     c_compile(cfiles);                      // compile cpp-files
  320. }
  321.  
  322. /*
  323.                                 C P P M A K E . C
  324.  
  325.     CPP files are processed by stdmake.
  326.  
  327.     Arguments of CPPMAKE:
  328.  
  329.     cpp_make(
  330.         string mainfile,    : name of the main .cpp file, or "" for library
  331.                               maintenance
  332.         string library,     : name of the local library to use/create
  333.                 (without lib prefix, .a suffix
  334.                  if main is given here, libmain.a is created)
  335.         string exe,         : (path) name of the exe file to create
  336.         )
  337.  
  338.     Both mainfile and library MUST be in the current directory
  339. */
  340.  
  341. void cpp_make(string mainfile, string library, string exe)
  342. {
  343.     int
  344.         n,
  345.         index;
  346.     list
  347.         classes;
  348.         
  349.     ofiles = "o/*.o";                       // std set of o-files
  350.  
  351.     classes = strtok(CLASSES, " ");         // list of classes
  352.  
  353.     if (n = sizeof(classes))
  354.         ofiles += " */o/*.o";               // set ofiles for NO_LIBRARY
  355.  
  356.     wild = sources;
  357.                         // make library name
  358.     libxxxa = chdir(".") + "lib" + library + ".a";
  359.  
  360.                                             // first process all classes
  361.     for (index = 0; index < n; index++)
  362.     {                   
  363.         current = element(index, classes);  // next class to process
  364.         chdir(current);                     // change to directory
  365.  
  366.         current = "subdir " + current;
  367.         std_cpp (libxxxa);                // compile all files
  368.         chdir( "..");                     // go back to parent dir
  369.     }
  370.  
  371.  
  372.     current = "auxiliary " + wild + " files";
  373.     std_cpp (libxxxa);                    // compile all files in current dir
  374.     
  375.     for (index = 0; index < n; index++)
  376.     {
  377.         current = element(index, classes);  // determine class name
  378.         chdir( current);                  // chdir to a class directory.
  379. #ifndef NO_LIBRARY
  380.     prefix_class((string)index);      // prefix class-number for .o files
  381.         updatelib(libxxxa);
  382. #endif
  383.         chdir( "..");                     // go back to parent dir
  384.     }
  385.  
  386.     current = "";                           // no class anymore
  387.  
  388. #ifndef NO_LIBRARY
  389.     updatelib(libxxxa);                // update lib in current dir
  390. #endif 
  391.  
  392.     if (mainfile != "")                     // mainfile -> do link
  393.     {
  394.         link(library, exe);
  395.         printf
  396.     (
  397.         "\nProgram construction completed.\n"
  398.         "\n"
  399.     );
  400.     }
  401. }
  402. /*
  403.                         S E T L I B S . I M
  404. */
  405. void setlibs()
  406. {       
  407.     int
  408.         n,
  409.         index;
  410.     list
  411.         cut;
  412.         
  413.     cut = strtok(LIBS, " ");        // cut op libraries
  414.     n = sizeof(cut);
  415.     for (index = 0; index < n; index++)
  416.         libs += " -l" + element(index, cut);
  417.     
  418.     cut = strtok(LIBPATH, " ");     // cut up the paths
  419.     n = sizeof(cut);
  420.     for (index = 0; index < n; index++)
  421.         libpath += " -L" + element(index, cut);
  422. }
  423.  
  424.  
  425. int main()
  426. {
  427.     setclasses();
  428.  
  429.     echo(ECHO_REQUEST);
  430.  
  431.     sources = "*.c";
  432.  
  433.     setlibs();
  434.  
  435. #ifdef GDB
  436.     copt = "-g";
  437. #else
  438.     copt = "-O2";
  439. #endif
  440.  
  441. #ifdef PROGRAM
  442.     cpp_make
  443.     (
  444.         "lib.cc",          // program source
  445.         "lib",                    // program library
  446.         "lib"                     // binary program
  447.     );
  448. #else
  449.     cpp_make
  450.     (
  451.         "",
  452.         "rss",                   // program library
  453.         ""
  454.     );
  455. #endif
  456.     return (0);
  457. }
  458.